﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace BitButterECS.System
{
	[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
	public class CacheComponentAttribute : Attribute
	{
		public CacheComponentAttribute(Type entity, params Type[] components)
		{
			Entity = entity;
			Components = components;
		}

		public Type Entity { get; }
		public Type[] Components { get; }

		public static IEnumerable<Tuple<Type, Type>> GetCacheComponents(object system)
		{
			var result = new List<Tuple<Type, Type>>();
			var attrs = system.GetType().GetCustomAttributes(typeof(CacheComponentAttribute), true);
			if (attrs.Length > 0)
			{
				foreach (CacheComponentAttribute attr in attrs)
				{
					if (attr != null)
					{
						result.AddRange(attr.Components.Select(x => new Tuple<Type, Type>(attr.Entity, x)));
					}
				}
			}
			return result;
		}
	}
}
